package com.yiwei.hdb.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.hdb.R;
import com.yiwei.hdb.activity.Application;

import java.util.Stack;

import es.dmoral.toasty.Toasty;


/**
 * Created by Master.Dong on 2016/4/5 0005.
 */
public class ToastUtil {
    private static ToastUtil instance;
    private static Toast mToast;
    private static TextView textView;

    /**
     * @param context  上下文
     * @param text     内容
     * @param duration 时长
     * @return
     */
    public static ToastUtil makeText(Context context, String text,
                                     int duration) {
//        Toast toast = new Toast(Application.getInstance());
//
//        // 获取LayoutInflater对象
//        LayoutInflater inflater = (LayoutInflater) Application.getInstance()
//                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        // 由layout文件创建一个View对象
//        View layout = inflater.inflate(R.layout.toast, null);
//
//        TextView textView = (TextView) layout.findViewById(R.id.text);
//        textView.setText(text);
//
//        toast.setView(layout);
//        toast.setGravity(Gravity.BOTTOM, 0, 200);
//        toast.setDuration(duration);
        show(context, text);
        return instance;
    }


    public static ToastUtil makeText(Context context, String text) {
//        Toast toast = new Toast(Application.getInstance());
//
//        // 获取LayoutInflater对象
//        LayoutInflater inflater = (LayoutInflater) Application.getInstance()
//                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        // 由layout文件创建一个View对象
//        View layout = inflater.inflate(R.layout.toast, null);
//
//        TextView textView = (TextView) layout.findViewById(R.id.text);
//        textView.setText(text);
//
//        toast.setView(layout);
//        toast.setGravity(Gravity.BOTTOM, 0, 200);
//        toast.setDuration(Toast.LENGTH_SHORT);
//        toast.show();
        show(context, text);
        return instance;
    }


    @SuppressLint("ShowToast")

    private static void show(Context context, CharSequence text) {

        if (mToast != null) {
            textView.setText(text);
        } else {
            mToast = new Toast(Application.getInstance());
            LayoutInflater inflater = (LayoutInflater) Application.getInstance()
                    .getSystemService("layout_inflater");
//            View toastLayout= ((LayoutInflater)context.getSystemService("layout_inflater")).inflate(es.dmoral.toasty.R.layout.toast_layout, (ViewGroup)null);
            // 由layout文件创建一个View对象
            View layout = inflater.inflate(R.layout.toast, null);
            textView = (TextView) layout.findViewById(R.id.text);
            textView.setText(text);

            mToast.setView(layout);
            mToast.setGravity(Gravity.BOTTOM, 0, 200);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();

//        Toasty.normal(context, text).show();
    }

    public static ToastUtil show() {
        return instance;
    }

    /**
     * @param context  上下文
     * @param text     内容
     * @param duration 时长
     * @return
     */
//    public static Toast makeText(Context context, CharSequence text,
//                                 int duration, int x, int y) {
//        Toast toast = new Toast(context);
//
//        // 获取LayoutInflater对象
//        LayoutInflater inflater = (LayoutInflater) context
//                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        // 由layout文件创建一个View对象
//        View layout = inflater.inflate(R.layout.toast, null);
//
//        TextView textView = (TextView) layout.findViewById(R.id.text);
//        textView.setText(text);
//
//        toast.setView(layout);
//        toast.setGravity(Gravity.CENTER, x, y);
//        toast.setDuration(duration);
//
//        return toast;
//    }


    /**
     * @param context  上下文
     * @param resId    图片资源
     * @param text     内容
     * @param duration 时长
     * @return
     */
//    public static Toast makeText(Context context, int resId, CharSequence text,
//                                 int duration) {
//        Toast toast = new Toast(context);
//
//        // 获取LayoutInflater对象
//        LayoutInflater inflater = (LayoutInflater) context
//                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        // 由layout文件创建一个View对象
//        View layout = inflater.inflate(R.layout.toast, null);
//
//        ImageView imageView = (ImageView) layout.findViewById(R.id.image);
//        TextView textView = (TextView) layout.findViewById(R.id.text);
//
//        imageView.setVisibility(View.VISIBLE);
//        imageView.setImageResource(resId);
//        textView.setText(text);
//
//        toast.setView(layout);
//        toast.setGravity(Gravity.BOTTOM, 0, 200);
//        toast.setDuration(duration);
//
//        return toast;
//    }




}
